﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using Ext.Net;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Interfaces;

#endregion

namespace Sharp.Web
{
    /// <summary>
    /// 树形构建器
    /// </summary>
    /// <typeparam name="TTreeEntity"></typeparam>
    public class TreeBuilder2<TTreeEntity>
        where TTreeEntity : ITreeEntity2
    {
        /// <summary>
        ///     数据转换
        /// </summary>
        public Action<TTreeEntity, Node> ChangeData;

        /// <summary>
        ///     构建ExtTreeNode数据源
        /// </summary>
        /// <param name="table">数据</param>
        /// <param name="node">树节点</param>
        /// <param name="parentValue">The parent value.</param>
        /// <exception cref="System.Exception">
        ///     ChangeData未实现
        ///     or
        ///     有死循环的危险。
        /// </exception>
        public void Build(IEnumerable<TTreeEntity> table, Node node, string parentValue)
        {
            var rows = table.Where(x => x.ParentCode == parentValue).OrderBy(x => x. Code);
            foreach (var row in rows)
            {
                var _node = new Node();

#if DEBUG
                if (ChangeData == null)
                {
                    throw new Exception("ChangeData未实现");
                }
#endif


                ChangeData(row, _node);
                node.Children.Add(_node);
#if DEBUG
                //if (_node.Id == parentValue)
                //{
                //    throw new Exception("有死循环的危险。");
                //}
#endif
                _node.Leaf = true;
                node.Leaf = !node.Children.Any();
                Build(table, _node, row.Code);
            }
        }
    }
    /// <summary>
    /// 树形构建器
    /// </summary>
    /// <typeparam name="TTreeEntity"></typeparam>
    public class TreeBuilder<TTreeEntity>
        where TTreeEntity : ITreeEntity
    {
        /// <summary>
        ///     数据转换
        /// </summary>
        public Action<TTreeEntity, Node> ChangeData;


        /// <summary>
        ///     构建ExtTreeNode数据源
        /// </summary>
        /// <param name="table">数据</param>
        /// <param name="node">树节点</param>
        /// <param name="parentValue">The parent value.</param>
        /// <exception cref="System.Exception">
        ///     ChangeData未实现
        ///     or
        ///     有死循环的危险。
        /// </exception>
        public void Build(IEnumerable<TTreeEntity> table, Node node, Guid? parentValue)
        {
            var rows = table.Where(x => x.ParentId == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new Node();

#if DEBUG
                if (ChangeData == null)
                {
                    throw new Exception("ChangeData未实现");
                }
#endif


                ChangeData(row, _node);
                node.Children.Add(_node);
#if DEBUG
                //if (_node.Id == parentValue)
                //{
                //    throw new Exception("有死循环的危险。");
                //}
#endif
                _node.Leaf = true;
                node.Leaf = !node.Children.Any();
                Build(table, _node, row.Id);
            }
        }
    }
}